// 模拟数据库数据
const posts = [
  {
    id: 1,
    title: "Vue.js 开发技巧",
    excerpt: "掌握 Vue.js 开发中的实用技巧和最佳实践，提升开发效率和代码质量。",
    category: "技术",
    date: "2024-01-15",
    image: "https://picsum.photos/400/300?random=1",
    tags: ["Vue.js", "前端", "最佳实践"],
    slug: "vuejs-development-tips",
    readTime: 8
  },
  {
    id: 2,
    title: "CSS Grid 布局实战技巧",
    excerpt: "掌握 CSS Grid 布局的核心概念和实用技巧，创建响应式网页布局变得简单高效。",
    category: "前端",
    date: "2024-01-12",
    image: "https://picsum.photos/400/300?random=2",
    tags: ["CSS", "布局", "响应式"],
    slug: "css-grid-layout-guide",
    readTime: 6
  },
  {
    id: 3,
    title: "JavaScript 异步编程模式",
    excerpt: "从回调函数到 async/await，全面了解 JavaScript 异步编程的演进和最佳实践。",
    category: "JavaScript",
    date: "2024-01-10",
    image: "https://picsum.photos/400/300?random=3",
    tags: ["JavaScript", "异步", "编程"],
    slug: "javascript-async-patterns",
    readTime: 10
  }
]

// GET /api/posts - 获取所有文章
export default defineEventHandler(async (event) => {
  const query = getQuery(event)
  
  // 支持分页
  const page = parseInt(query.page as string) || 1
  const limit = parseInt(query.limit as string) || 10
  const start = (page - 1) * limit
  const end = start + limit
  
  // 支持分类筛选
  const category = query.category as string
  let filteredPosts = posts
  
  if (category) {
    filteredPosts = posts.filter(post => post.category === category)
  }
  
  // 支持标签筛选
  const tag = query.tag as string
  if (tag) {
    filteredPosts = filteredPosts.filter(post => post.tags.includes(tag))
  }
  
  // 支持搜索
  const search = query.search as string
  if (search) {
    filteredPosts = filteredPosts.filter(post => 
      post.title.toLowerCase().includes(search.toLowerCase()) ||
      post.excerpt.toLowerCase().includes(search.toLowerCase())
    )
  }
  
  const paginatedPosts = filteredPosts.slice(start, end)
  
  return {
    posts: paginatedPosts,
    total: filteredPosts.length,
    page,
    limit,
    totalPages: Math.ceil(filteredPosts.length / limit)
  }
}) 